/**
 * 
 */
package lancs.mobilemedia.core.ui.controller;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import lancs.mobilemedia.core.ui.datamodel.MediaData;
import lancs.mobilemedia.core.ui.screens.NewLabelScreen;
import lancs.mobilemedia.core.util.Constants;


/**
 * @author E Figueiredo
 * Add in the Scenario 02
 */
public class MediaController implements CommandListener {

	private BaseController nextController;
	private MediaData media;
	private NewLabelScreen screen;

	public MediaController (MediaData image, BaseController nextController) {
		this.media = image;
		this.nextController = nextController;
	}

	public boolean handleCommand(Command c, Displayable d) {
		String label = c.getLabel();
//      	System.out.println( "<* PhotoController.handleCommand() *> " + label);

		/** Case: Save new Photo Label */
		if (label.equals("Save")) {
	      	System.out.println( "<* PhotoController.handleCommand() *> Save Photo Label = "+this.screen.getLabelName());
			this.getMedia().setImageLabel(this.screen.getLabelName());
			this.nextController.updateImage(media);
			goToPreviousScreen();
			return true;
		} else if (label.equals("Cancel")) {
			goToPreviousScreen();
			return true;
		}
		return true;
	}

	public void commandAction(Command c, Displayable d) {
		handleCommand(c, d);
	}

   /**
     * Go to the previous screen
	 */
    private void goToPreviousScreen() {
	    System.out.println("<* PhotoController.goToPreviousScreen() *>");
	    this.nextController.showImageList(null, false, false);
	    this.nextController.setCurrentScreenName(Constants.IMAGELIST_SCREEN);
    } 

	/**
	 * @param image the image to set
	 */
	public void setMedia(MediaData media) {
		this.media = media;
	}

	/**
	 * @return the image
	 */
	public MediaData getMedia() {
		return media;
	}

	public void setScreen(NewLabelScreen screen) {
		this.screen = screen;
	}

	public NewLabelScreen getScreen() {
		return screen;
	}

}
